"ui";

var Utils = require('./Utils.js')

var flag = true
var storage = storages.create('yys')
var num = storage.get('num', 3)    // 地域鬼王数量
var level = storage.get('level', 15)    // 地域鬼王等级
var wt_num = storage.get('wt_num', 30)    // 挖土次数
var wtbyq_num = storage.get('wtbyq_num', 30)    // 挖土次数-被邀请
var my = storage.get('my', '')   // 密钥
var high = 50
var wechat, qq   // 微信、QQ
threads.start(function () {
    let data = Utils.getRequest('http://xjj0427.icu/wp-json/xjj/contacts')
    data = data.body.json()
    wechat = data.wechat
    qq = data.qq
})

var baseColor = '#4fb3ff'
var white_color = '#ffffff'

ui.statusBarColor(baseColor)  // 设置当前界面的状态栏颜色

ui.layout(
    <frame>
        <vertical>
            <appbar>
                <toolbar title="痒痒鼠" bg="{{baseColor}}" />
            </appbar>
            <card id="dygw" w="*" h="{{ high }}" margin="10 5" cardCornerRadius="2dp"
                cardElevation="1dp" foreground="?selectableItemBackground">
                <horizontal gravity="center_vertical">
                    <View bg="#f44336" h="*" w="10" />
                    <vertical padding="10 8" h="auto" w="0" layout_weight="1">
                        <text text="地域鬼王" textColor="#222222" textSize="20" maxLines="1" />
                    </vertical>
                </horizontal>
            </card>
            <card id="ltp" w="*" h="{{ high }}" margin="10 5" cardCornerRadius="2dp"
                cardElevation="1dp" foreground="?selectableItemBackground">
                <horizontal gravity="center_vertical">
                    <View bg="#4caf50" h="*" w="10" />
                    <vertical padding="10 8" h="auto" w="0" layout_weight="1">
                        <text text="寮突破" textColor="#222222" textSize="20" maxLines="1" />
                    </vertical>
                </horizontal>
            </card>
            <card id="wt" w="*" h="{{ high }}" margin="10 5" cardCornerRadius="2dp"
                cardElevation="1dp" foreground="?selectableItemBackground">
                <horizontal gravity="center_vertical">
                    <View bg="#ff5722" h="*" w="10" />
                    <vertical padding="10 8" h="auto" w="0" layout_weight="1">
                        <text text="挖土" textColor="#222222" textSize="20" maxLines="1" />
                    </vertical>
                </horizontal>
            </card>
            <card id="wtbyq" w="*" h="{{ high }}" margin="10 5" cardCornerRadius="2dp"
                cardElevation="1dp" foreground="?selectableItemBackground">
                <horizontal gravity="center_vertical">
                    <View bg="#985722" h="*" w="10" />
                    <vertical padding="10 8" h="auto" w="0" layout_weight="1">
                        <text text="挖土（被邀请）" textColor="#222222" textSize="20" maxLines="1" />
                    </vertical>
                </horizontal>
            </card>
            <card id="jjtp" w="*" h="{{ high }}" margin="10 5" cardCornerRadius="2dp"
                cardElevation="1dp" foreground="?selectableItemBackground">
                <horizontal gravity="center_vertical">
                    <View bg="#2196f3" h="*" w="10" />
                    <vertical padding="10 8" h="auto" w="0" layout_weight="1">
                        <text text="结界突破" textColor="#222222" textSize="20" maxLines="1" />
                    </vertical>
                </horizontal>
            </card>
            <card id="miyao" w="*" h="{{ high }}" margin="10 5" cardCornerRadius="2dp"
                cardElevation="1dp" foreground="?selectableItemBackground">
                <horizontal gravity="center_vertical">
                    <View bg="#600aa2" h="*" w="10" />
                    <vertical padding="10 8" h="auto" w="0" layout_weight="1">
                        <text text="密钥" textColor="#222222" textSize="20" maxLines="1" />
                    </vertical>
                </horizontal>
            </card>
            <card id="contact" w="*" h="{{ high }}" margin="10 5" cardCornerRadius="2dp"
                cardElevation="1dp" foreground="?selectableItemBackground">
                <horizontal gravity="center_vertical">
                    <View bg="#100aa2" h="*" w="10" />
                    <vertical padding="10 8" h="auto" w="0" layout_weight="1">
                        <text text="联系方式" textColor="#222222" textSize="20" maxLines="1" />
                    </vertical>
                </horizontal>
            </card>
            <card id="downImg" w="*" h="{{ high }}" margin="10 5" cardCornerRadius="2dp"
                cardElevation="1dp" foreground="?selectableItemBackground">
                <horizontal gravity="center_vertical">
                    <View bg="#4caf50" h="*" w="10" />
                    <vertical padding="10 8" h="auto" w="0" layout_weight="1">
                        <text text="下载必要文件" textColor="#222222" textSize="20" maxLines="1" />
                    </vertical>
                </horizontal>
            </card>
        </vertical>
    </frame>
)

// 地域鬼王
ui.dygw.click(view => {
    var dialog = new android.app.AlertDialog.Builder(activity).create()
    dialog.setView(new android.widget.EditText(context))
    dialog.show()
    dialog.setCancelable(false)
    var window = dialog.getWindow()
    window.setDimAmount(0.6)
    var vv = ui.inflate(
        <vertical>
            <vertical margin="25 25 25 10">
                <linear>
                    <text gravity="center" color="#555555" size="17" text="数量 " />
                    <input textColor="#000000" inputType="number" id="num" w="*" text="{{num}}" />
                </linear>
                <linear>
                    <text gravity="center" color="#555555" size="17" text="等级" />
                    <input textColor="#000000" id="level" w="*" text="{{level}}" />
                </linear>
            </vertical>
            <relative>
                <button id="cancle" layout_alignParentLeft="true" text="取消" style="Widget.AppCompat.Button.Borderless.Colored" w="auto" />
                <button id="login" layout_alignParentRight="true" text="开始" style="Widget.AppCompat.Button.Borderless.Colored" w="auto" />
            </relative>
        </vertical>
    )
    window.setContentView(vv)
    vv.cancle.click(() => { dialog.dismiss() })

    vv.login.click(() => {
        let my = storage.get('my')
        !flag && threads.start(function () { flag = Utils.isAlive(my) })
        setTimeout(() => {
            if (flag) {
                if (vv.num.text() > 0 && vv.num.text() < 4) {
                    if (vv.level.text() > 0 && vv.level.text() <= 60) {
                        storage.put('num', vv.num.text())
                        storage.put('level', vv.level.text())
                        launchApp('阴阳师')
                        setTimeout(() => {
                            engines.execScriptFile("./地域鬼王.js")
                            dialog.dismiss()
                            Utils.stopJB()
                        }, 5000)
                    } else {
                        toast('等级可输入的范围 1 ~ 60 ！')
                        return
                    }
                } else {
                    toast('数量可输入的范围 1 ~ 3 ！')
                    return
                }
            } else {
                dialog.dismiss()
                toastLog('密钥已失效或不存在')
            }
        }, 2000)
    })
})

// 寮突破
ui.ltp.click(view => {
    let my = storage.get('my')
    !flag && threads.start(function () { flag = Utils.isAlive(my) })
    setTimeout(() => {
        if (flag) {
            confirm("开始寮突破？").then(flag => {
                if (flag) {
                    launchApp('阴阳师')
                    setTimeout(() => {
                        engines.execScriptFile("./寮突破.js")
                        Utils.stopJB()
                    }, 5000)
                }
            })
        } else {
            toastLog('密钥已失效或不存在')
        }
    }, 2000)
})

// 挖土
ui.wt.click(view => {
    var dialog = new android.app.AlertDialog.Builder(activity).create()
    dialog.setView(new android.widget.EditText(context))
    dialog.show()
    dialog.setCancelable(false)
    var window = dialog.getWindow()
    window.setDimAmount(0.6)
    var vv = ui.inflate(
        <vertical>
            <vertical margin="25 25 25 10">
                <linear>
                    <text gravity="center" color="#555555" size="17" text="要挖多少次： " />
                    <input textColor="#000000" inputType="number" id="wt_num" w="*" text="{{wt_num}}" />
                </linear>
            </vertical>
            <relative>
                <button id="cancle" layout_alignParentLeft="true" text="取消" style="Widget.AppCompat.Button.Borderless.Colored" w="auto" />
                <button id="start" layout_alignParentRight="true" text="开始" style="Widget.AppCompat.Button.Borderless.Colored" w="auto" />
            </relative>
        </vertical>
    )
    window.setContentView(vv)
    vv.cancle.click(() => { dialog.dismiss() })

    vv.start.click(() => {
        let my = storage.get('my')
        !flag && threads.start(function () { flag = Utils.isAlive(my) })
        setTimeout(() => {
            if (flag) {
                if (vv.wt_num.text() > 0) {
                    storage.put('wt_num', vv.wt_num.text())
                    launchApp('阴阳师')
                    setTimeout(() => {
                        engines.execScriptFile("./魂十一.js")
                        dialog.dismiss()
                        Utils.stopJB()
                    }, 5000)
                } else {
                    toast('挖土次数至少是 1 次！')
                    return
                }
            } else {
                dialog.dismiss()
                toastLog('密钥已失效或不存在')
            }
        }, 2000)
    })
})

// 挖土（被邀请）
ui.wtbyq.click(view => {
    var dialog = new android.app.AlertDialog.Builder(activity).create()
    dialog.setView(new android.widget.EditText(context))
    dialog.show()
    dialog.setCancelable(false)
    var window = dialog.getWindow()
    window.setDimAmount(0.6)
    var vv = ui.inflate(
        <vertical>
            <vertical margin="25 25 25 10">
                <linear>
                    <text gravity="center" color="#555555" size="17" text="要挖多少次： " />
                    <input textColor="#000000" inputType="number" id="wtbyq_num" w="*" text="{{wtbyq_num}}" />
                </linear>
            </vertical>
            <relative>
                <button id="cancle" layout_alignParentLeft="true" text="取消" style="Widget.AppCompat.Button.Borderless.Colored" w="auto" />
                <button id="start" layout_alignParentRight="true" text="开始" style="Widget.AppCompat.Button.Borderless.Colored" w="auto" />
            </relative>
        </vertical>
    )
    window.setContentView(vv)
    vv.cancle.click(() => { dialog.dismiss() })

    vv.start.click(() => {
        let my = storage.get('my')
        !flag && threads.start(function () { flag = Utils.isAlive(my) })
        setTimeout(() => {
            if (flag) {
                if (vv.wtbyq_num.text() > 0) {
                    storage.put('wtbyq_num', vv.wtbyq_num.text())
                    launchApp('阴阳师')
                    setTimeout(() => {
                        engines.execScriptFile("./魂十一-被邀请.js")
                        dialog.dismiss()
                        Utils.stopJB()
                    }, 5000)
                } else {
                    toast('挖土次数至少是 1 次！')
                    return
                }
            } else {
                dialog.dismiss()
                toastLog('密钥已失效或不存在')
            }
        }, 2000)
    })
})

// 结界突破
ui.jjtp.click(view => {
    let my = storage.get('my')
    !flag && threads.start(function () { flag = Utils.isAlive(my) })
    setTimeout(() => {
        if (flag) {
            confirm("开始突破？").then(flag => {
                if (flag) {
                    launchApp('阴阳师')
                    setTimeout(() => {
                        engines.execScriptFile("./结界突破.js")
                        Utils.stopJB()
                    }, 5000)
                }
            })
        } else {
            toastLog('密钥已失效或不存在')
        }
    }, 2000)
})

// 密钥
ui.miyao.click(view => {
    var dialog = new android.app.AlertDialog.Builder(activity).create()
    dialog.setView(new android.widget.EditText(context))
    dialog.show()
    dialog.setCancelable(false)
    var window = dialog.getWindow()
    window.setDimAmount(0.6)
    var vv = ui.inflate(
        <vertical>
            <vertical margin="25 25 25 10">
                <linear>
                    <text gravity="center" color="#555555" size="17" text="密钥" />
                    <input textColor="#000000" inputType="textVisiblePassword" id="my" w="*" text="{{my}}" />
                </linear>
            </vertical>
            <relative>
                <button id="cancle" layout_alignParentLeft="true" text="取消" style="Widget.AppCompat.Button.Borderless.Colored" w="auto" />
                <button id="save" layout_alignParentRight="true" text="保存" style="Widget.AppCompat.Button.Borderless.Colored" w="auto" />
            </relative>
        </vertical>
    )
    window.setContentView(vv)
    vv.cancle.click(() => { dialog.dismiss() })

    vv.save.click(() => {
        my = vv.my.text()
        storage.put('my', my)
        if (storage.get('my') === vv.my.text()) {
            toastLog('密钥保存成功')
        } else {
            toastLog('密钥保存失败')
        }
        dialog.dismiss()
    })
})

// 联系方式
ui.contact.click(view => {
    var dialog = new android.app.AlertDialog.Builder(activity).create()
    dialog.setView(new android.widget.EditText(context))
    dialog.show()
    dialog.setCancelable(false)
    var window = dialog.getWindow()
    window.setDimAmount(0.6)
    var vv = ui.inflate(
        <vertical>
            <vertical margin="25 25 25 10">
                <linear>
                    <text gravity="center" color="#555555" size="17" text="微信：" />
                    <text gravity="center" color="#555555" size="17" text="{{wechat}}" />
                </linear>
                <linear>
                    <text gravity="center" color="#555555" size="17" text="QQ：" />
                    <text gravity="center" color="#555555" size="17" text="{{qq}}" />
                </linear>
            </vertical>
            <relative>
                <button id="cancle" layout_alignParentRight="true" text="取消" style="Widget.AppCompat.Button.Borderless.Colored" w="auto" />
            </relative>
        </vertical>
    )
    window.setContentView(vv)
    vv.cancle.click(() => { dialog.dismiss() })
})

// 下载必要文件
ui.downImg.click(view => {
    files.ensureDir('/sdcard/Pictures/' + device.getAndroidId() + '/yys/')
    const urlHandler = name => {
        return 'https://xjj0427.icu/wp-content/uploads/2022/02/' + name + '.png'
    }
    const targetPath = name => {
        return '/sdcard/Pictures/' + device.getAndroidId() + '/yys/' + name + '.png'
    }
    threads.start(function () {
        let pics = [
            '叉号',
            '地域鬼王',
            '返回二',
            '返回一',
            '好友协作申请叉号',
            '加',
            '减',
            '奖励',
            '进攻',
            '秘闻挑战',
            '确定',
            '筛选',
            '胜利',
            '失败',
            '刷新',
            '挑战',
            '挑战秘闻',
            '挑战挖土',
            '未通关',
            '现世妖约',
            '选择挑战',
            '勋章空',
            '勋章空备用',
            '勋章实',
            '勋章实备用',
            '妖怪之里',
            '准备',
            '自动',
        ]
        let count = 0
        pics.map(item => {
            let imgObj = images.load(urlHandler(item))
            images.save(imgObj, targetPath(item), "png", 100)
            imgObj.recycle()
            count++
            log(item)
        })
        count === pics.length ? toastLog('下载完成！') : toastLog('下载失败！')
    })
})
